﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AppAcc.Models;
using DevComponents.DotNetBar;

namespace AppAcc.Views.MuaBanHangHoa
{
    public partial class FormChungTuBanHang : DevComponents.DotNetBar.RibbonForm
    {
        private AppAccContext db = new AppAccContext();
        private string idtam = "";
        public FormChungTuBanHang()
        {
            InitializeComponent();
        }

        private void dgvChungTu_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var index = e.RowIndex;
            DataGridViewRow row = dgvChungTu.Rows[index];
            idtam = row.Cells[0].Value.ToString();
            KiemTra();
            dgvCTChungTu.DataSource =
                db.PhieuNhapKhoHangChiTiets.Where(t => t.PhieuNhapKhoId == idtam).ToList();
            FilldgvChiTietTChungTu();
        }

        private void dgvChungTu_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var index = e.RowIndex;
            DataGridViewRow row = dgvChungTu.Rows[index];
            idtam = row.Cells[0].Value.ToString();
            KiemTra();
            //var f = new FormChungTuNhapKhoHangChinhSua();
            //f.idct = idtam;
            //f.ShowDialog();
        }

        private void FormChungTuBanHang_Load(object sender, EventArgs e)
        {
            GetListChungTuBanHang();
        }

        private void bttThemMoi_Click(object sender, EventArgs e)
        {

        }

        private void bttChinhSua_Click(object sender, EventArgs e)
        {

        }

        private void bttXoa_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void bttLamMoi_Click(object sender, EventArgs e)
        {
            GetListChungTuBanHang();
        }

        private void bttTimKiem_Click(object sender, EventArgs e)
        {
            TimKiem();
        }
        private void dgvChungTu_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                var ht = dgvChungTu.HitTest(e.X, e.Y);
                var rowindex = ht.RowIndex;
                var row = dgvChungTu.Rows[rowindex];
                idtam = row.Cells[0].Value.ToString();

                if (ht.ColumnIndex != -1 && ht.RowIndex != -1)
                {
                    var Menu = new ContextMenuStrip();
                    var lammoi = new ToolStripMenuItem("Làm mới");
                    lammoi.MouseDown += bttLamMoi_Click;
                    var edit = new ToolStripMenuItem("Chỉnh sửa");
                    edit.MouseDown += bttChinhSua_Click;
                    var xoa = new ToolStripMenuItem("Xóa");
                    xoa.MouseDown += bttXoa_Click;
                    Menu.Items.AddRange(new ToolStripItem[] { lammoi });
                    Menu.Items.AddRange(new ToolStripItem[] { edit });
                    Menu.Items.AddRange(new ToolStripItem[] { xoa });
                    dgvChungTu.ContextMenuStrip = Menu;
                }
                else
                    dgvChungTu.ContextMenuStrip = null;
            }
        }
        void GetListChungTuBanHang()
        {
            dgvChungTu.DataSource = db.PhieuBanHangs.ToList();
            FilldgvChungTu();
            Reset();
            KiemTra();
        }
        void Delete()
        {
            if (idtam != "")
            {
                var t = db.PhieuBanHangs.Find(idtam);
                if (
                    MessageBox.Show("Bạn muốn xóa chứng từ: " + t.Id + "!", "Xác nhận", MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        db.PhieuBanHangs.Remove(t);
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }

                }
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu để xóa!");
            }
        }
        void Reset()
        {
            idtam = "";
            bttChinhSua.Enabled = false;
            bttXoa.Enabled = false;
        }
        void KiemTra()
        {
            if (idtam != "")
            {
                bttChinhSua.Enabled = true;
                bttXoa.Enabled = true;
            }
            else
            {
                bttChinhSua.Enabled = false;
                bttXoa.Enabled = false;
            }
        }

        void TimKiem()
        {
            dgvChungTu.DataSource =
                db.PhieuBanHangs.Where(t => t.NgayChungTu == Convert.ToDateTime(dtiNgayChungTu.Value));
            Reset();
            KiemTra();
        }

        void FilldgvChungTu()
        {
            var n = dgvChungTu.Columns["DoiTacId"] as DataGridViewComboBoxColumn;
            n.DataSource = db.DoiTacs.ToList();
            n.DisplayMember = "TenDoiTac";
            n.ValueMember = "Id";

            var nv = dgvChungTu.Columns["NhanVienId"] as DataGridViewComboBoxColumn;
            nv.DataSource = db.NhanViens.ToList();
            nv.DisplayMember = "TenNhanVien";
            nv.ValueMember = "Id";
        }

        void FilldgvChiTietTChungTu()
        {
            var n = dgvChungTu.Columns["TaiKhoanNo"] as DataGridViewComboBoxColumn;
            n.DataSource = db.TaiKhoans.ToList();
            n.DisplayMember = "MaTaiKhoan";
            n.ValueMember = "Id";

            var c = dgvChungTu.Columns["TaiKhoanCo"] as DataGridViewComboBoxColumn;
            c.DataSource = db.TaiKhoans.ToList();
            c.DisplayMember = "MaTaiKhoan";
            c.ValueMember = "Id";

            var h = dgvChungTu.Columns["HangHoaId"] as DataGridViewComboBoxColumn;
            h.DataSource = db.HangHoas.ToList();
            h.DisplayMember = "TenHangHoa";
            h.ValueMember = "Id";

            var t = dgvChungTu.Columns["NhomThueHangHoaId"] as DataGridViewComboBoxColumn;
            t.DataSource = db.NhomThueHangHoas.ToList();
            t.DisplayMember = "TenThue";
            t.ValueMember = "Id";
        }
    }
}